<template>
    <div class="bg-white px-3" style="margin: -20px; margin-top:-1rem; margin-bottom: 0!important;">
        <!-- 标签页的内容 -->
        <!-- 高级搜索-->
        <button-search placeholder='手机号/邮箱/会员名称' @search="searchEvent" :showSuperSearch="true">
            <!-- 会匹配和使用到slot name=left的插槽 -->
            <template #left>
                <el-button type="success" size="mini" @click="openModel(false)">添加会员</el-button>
            </template>
            <!-- 高级搜索的表单 -->
            <template #form>
                <!-- 表单 -->
                <el-form inline ref="form" :model="form" label-width="70px">
                    <el-form-item label="搜索内容" class="mb-0">
                        <el-input v-model="search.keyword" size="mini" placeholder="手机号/邮箱/会员名称">

                        </el-input>
                    </el-form-item>
                    <el-form-item label="会员等级" class="mb-0">
                        <el-select v-model="search.user_level_id" style="height: 28px; line-height: 28px;"
                            placeholder="选择会员状态">
                            <!-- 这里的user_level是从打印出来的e即后端获取到的 -->
                            <el-option v-for="(item, index) in user_level" :key="index" :label="item.name"
                                :value="item.id">

                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="发布时间" class="mb-0">
                        <el-date-picker size='middle' v-model='search.time' type='daterange' range-separator='至'
                            start-placeholder='开始日期' end-placeholder='结束日期'>
                        </el-date-picker>
                    </el-form-item> -->

                    <el-form-item>
                        <el-button type="info" size="mini" @click="searchEvent">搜索</el-button>
                        <el-button size="mini" @click="clearSearch">清空筛选条件</el-button>
                    </el-form-item>
                </el-form>
            </template>
        </button-search>

        <!-- 商品列表 -->
        <el-table :data="tableData" style="margin-top: 20px; background-color: antiquewhite;">
            <!-- 添加顶部间距 -->
            <el-table-column label="会员" width='230'>
                <template slot-scope="scope">
                    <div class="media">
                        <img :src="scope.row.avatar" style="width:40px; height:40px" alt="..." class="rounded-circle">
                        <div class="media-body ml-3">
                            <h6 class="mt-0">{{ scope.row.username }}
                                <el-button type='text' class="ml-auto" size='mini'>详情</el-button>
                            </h6>
                            用户id：{{ scope.row.id }}
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="会员等级" align="center">
                <template slot-scope="scope">
                    {{ scope.row.user_level.name }}
                </template>
            </el-table-column>
            <el-table-column label="注册/登陆" align="center">
                <template slot-scope="scope">
                    <div>注册时间： {{ scope.row.create_time }}</div>
                    <div>最后登录： {{ scope.row.last_login_time }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" align="center">
                <template slot-scope="scope">

                    <el-button size="mini" plain @click="changeStatus(scope.row)"
                        :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '启用' : '禁用'
                        }}</el-button>

                </template>
            </el-table-column>

            <el-table-column prop="ischeck" label="操作" align="center" width="250">
                <template slot-scope="scope">
                    <!-- slot-scope="scope"创建了一个名为scope的新作用域，
                             它包含了子组件传递给插槽的所有数据。然后我们可以使用v-for指令来遍历scope.items。 -->
                    <el-button type="text" icon="el-icon-arrow" size="mini" plain @click="openModel(scope)">修改
                    </el-button>
                    <!-- 定义在混入中的delItem方法传入的是对象item，因此此处必须传scope.row -->
                    <el-button type="text" icon="el-icon-arrow" size="mini" plain @click="delItem(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 底部 -->
        <div style="height: 60px;">
            <el-footer class="border-top d-flex align-items-center px-0 position-fixed bg-white"
                style="bottom:0;left: 200px;right: 0; z-index: 100;">
                <!-- flex:1占剩余的所有空间 -->
                <div style="flex: 1;">
                    <el-pagination :current-change="page.current" :page-sizes="page.sizes" :page-size="page.size"
                        layout="total, sizes, prev, pager, next, jumper" :total="page.total"
                        @size-change="handleSizeChange" @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </el-footer>

            <!-- 新增、修改规格的模态框 -->
            <el-dialog title="添加会员" :visible.sync="createModel" top="5vh" width="40%">
                <!-- 表单内容 -->
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="用户名">
                        <el-input v-model="form.username" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="form.password" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称">
                        <el-input v-model="form.nickname" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="头像">
                        <!-- 图片选择 -->
                        <div>
                            <!-- 没有头像时显示的 -->
                            <span v-if="!form.avatar" class="btn btn-light border" @click="chooseImage">
                                <i class="el-icon-plus"></i>
                            </span>
                            <!-- 有头像时显示的 -->
                            <img v-else :src="form.avatar" class="border" style="width:45px;height:45px;cursor:pointer"
                                @click="chooseImage" />
                        </div>
                    </el-form-item>
                    <el-form-item label="会员等级">
                        <el-select v-model="form.user_level_id" style="height: 28px; line-height: 28px;"
                            placeholder="选择会员等级">
                            <!-- 这里的user_level是从打印出来的e即后端获取到的 -->
                            <el-option v-for="(item, index) in user_level" :key="index" :label="item.name"
                                :value="item.id">

                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="手机">
                        <el-input v-model="form.phone" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱">
                        <el-input v-model="form.email" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-radio-group v-model="form.status" size="mini">
                            <el-radio :label="1" border>启用</el-radio>
                            <el-radio :label="0" border>禁用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="createModel = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import buttonSearch from '@/components/common/button-search.vue';
import common from '@/common/mixins/common.js';

export default {
    name: 'userList',
    inject: ['app', 'layout'],
    mixins: [common],
    components: { buttonSearch },
    data() {
        return {
            preUrl: 'user',
            search: {
                keyword: '',
                user_level_id: '', // 会员等级
                // group_id: '',
                // time: '',
            },
            form: {
                username: '',
                password: 50,
                status: 1,
                nickname: 0,
                avatar: '',
                user_level_id: 3, // 默认会员等级
                phone: '',
                email: '',
            },
            tableData: [
                //     {
                //     id: 10,
                //     username: '用户名',
                //     avatar: require('@/assets/image.png'),
                //     level_id: 1,
                //     level: {
                //         id: 1,
                //         name: '普通会员'
                //     },
                //     create_time: '2019-07-24 27:29:07',
                //     update_time: '2019-07-24 27:29:07',
                //     status: 1,
                // }
            ],
            // currentPage: 1,
            createModel: false,
            editIndex: -1,

            // 获取会员等级,在高级搜索中
            user_level: [],
        };
    },
    created() {

    },
    methods: {
        getListResult(e) {
            // console.log('获取列表数据', e);

            // 获取列表数据
            this.tableData = e.list;
            // 获取会员等级,在高级搜索中
            this.user_level = e.user_level;
        },
        //  添加规格事件
        openModel(e = false) {
            // 如果e为false，说明是添加操作，执行添加操作
            if (!e) {
                // 初始化表单
                this.form = {
                    username: '',
                    password: 50,
                    status: 1,
                    nickname: 0,
                    avatar: '',
                    user_level_id: '',
                    phone: '',
                    email: '',
                }
                // 增加操作
                this.editIndex = -1
            } else {
                // 修改操作
                this.form = {
                    username: e.row.username,
                    password: "",
                    status: e.row.status,
                    nickname: e.row.nickname,
                    avatar: e.row.avatar,
                    user_level_id: e.row.user_level_id,
                    phone: e.row.phone,
                    email: e.row.email,
                }
                // 修改时设置
                this.editIndex = e.$index
            }
            this.createModel = true
        },
        submit() {
            let id = 0;
            // var msg = '添加'
            // if (this.editIndex == -1) {
            // 不全等于-1是修改操作，需要传id
            if (this.editIndex !== -1) {

                // this.form.level = {
                //     id: 1,
                //     name: '普通会员'
                // }
                // this.tableData.unshift(this.form)

                id = this.tableData[this.editIndex].id;
            }
            this.addOrEdit(this.form, id)
            // else {
            // msg = '修改'
            // let item = this.tableData[this.editIndex];
            // item.username = this.form.username,
            //     item.password = this.form.password,
            //     item.status = this.form.status,
            //     item.nickname = this.form.nickname,
            //     item.avatar = this.form.avatar,
            //     item.user_level_id = this.form.level,
            //     item.phone = this.form.phone,
            //     item.email = this.form.email,
            //     item.sex = this.form.sex
            // }
            this.createModel = false
            // this.$message({
            //     message: msg + '成功',
            //     type: 'success',
            // });
        },

        // 用来加载数据，监听点的是哪一个tab
        handleClick() {
            // console.log(this.activeName);
        },

        // 删除当前商品
        // delItem(scope) {
        //     this.$confirm('是否要删除该会员?', '提示', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning'
        //     }).then(() => {
        //         this.tableData.splice(scope, 1)
        //         this.$message({
        //             type: 'success',
        //             message: '删除成功!'
        //         });
        //     })
        // },

        // // 修改状态
        // changeStatus(item) {
        //     // 请求服务端修改状态代码：

        //     // 修改前端
        //     item.status = !item.status
        //     this.$message({
        //         message: item.status ? '启用成功' : '禁用成功',
        //         type: 'success',
        //         duration: 2000
        //     })
        // },
        onSubmit() {
            console.log('submit!');
        },
        // 清空筛选条件
        clearSearch() {
            this.search = {
                keyword: '',
                user_level_id: "", // 会员等级
                // group_id: '',
                // time: '',
            }
            // this.$refs.buttonSearch[this.tabIndex].closeSuperSearch();
            console.log('清空搜索条件');
        },

        // 获取列表请求分页的url
        getListUrl() {
            return `/admin/${this.preUrl}/${this.page.current}?limit=${this.page.size}&keyword=${this.search.keyword}&user_level_id=${this.search.user_level_id}`;
        },
        // 搜索事件
        searchEvent(e = false) {
            // 简单搜索
            if (typeof e === 'string') {
                // 将搜索关键字赋值给search.keyword
                // 如果e是字符串，说明是简单搜索
                this.search.keyword = e
                this.getList()
                return
            }

            // // 简单搜索,如果e有值
            // if (typeof e === 'string') {
            //     return console.log("简单搜索事件", e)
            // }
            // // 高级搜索事件
            // console.log("高级搜索")

            // 高级搜索
            this.getList()
        },
        // 选择头像
        // 选择图片
        chooseImage() {
            // 下面的chooseImage是来自于app.vue中的函数
            // 传递max参数为3，即最大选中chooseImage=3
            this.app.chooseImage((res) => {
                // 拿到当前第一个元素的url替换掉上方的span
                // console.log(res[0].url);
                // 修改image值对应到store中的good_create.js中的sku_card.list中的image的值
                this.form.avatar = res[0].url
            }, 3);
        },
    },
};
</script>
<style scoped>
.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
    display: table;
    content: "";
}

.clearfix:after {
    clear: both
}
</style>